-# locals: instance, id_prefix, seen_ids, is_root

-model_underscore = instance.class.name.underscore

-is_root = false if local_assigns[:is_root].nil?

-seen_ids = [] if local_assigns[:seen_ids].nil?
-id_prefix = "instance-#{instance.id}" if local_assigns[:id_prefix].nil?

-seen_this = seen_ids.include?(instance.id)

-systems = instance.systems
-sub_objects = instance.dependent_self_objects

-expand_this = (!seen_this && systems.count > 0) || (!seen_this && sub_objects.count > 0)

-summary_toggle_target = "#{id_prefix}-summary"
-objects_toggle_target = "#{id_prefix}-objects"

-main_toggle_target = expand_this ? objects_toggle_target : summary_toggle_target

-dependence_relationship_type = "#{model_underscore}_relies_upon_#{model_underscore}"
-process_relationship_type = "#{model_underscore}_has_process"

-has_dependence_relationship = instance.has_valid_relationship?(dependence_relationship_type)
-has_process_relationship = instance.has_valid_relationship?(process_relationship_type)

%li{ :class => instance.class.name.downcase, :'data-object-id' => instance.id, :'data-object-type' => instance.class.name.underscore }
  .item-main
    -if !is_root
      .openclose{ :class => !expand_this && 'lastchild' }
    .item-title{ :class => is_root && 'active' }
      .row-fluid
        =render :partial => 'object_leaf', :locals => { :instance => instance, :object_type => 'business', :object_icon => "grcicon-#{instance.class.name.downcase}" }
      .item-actions
        %a.btn.btn-mini{ "rel" => "tooltip", "data-placement" => "left", "title" => "Edit this object", 'data-modal-class' => 'modal-wide', 'data-form-target' => 'refresh', "data-toggle" => "modal-ajax-form", :href => url_for(:id => instance.id, :action => :edit, :controller => model_underscore.pluralize, :only_path => true) }
          %i.grcicon-edit
        -if has_dependence_relationship
          %a.btn.btn-mini{ :rel => "tooltip", :'data-placement' => "left", :title => "Link #{instance.class.name.titleize.downcase.pluralize} to this object", :'data-modal-class' => 'modal-wide', :'data-modal-reset' => 'reset', :'data-list-target' => 'refresh', :'data-toggle' => "modal-ajax-relationship-selector", :href => list_edit_flow_relationships_path(:object_id => instance.id, :object_type => instance.class.name, :related_model => instance.class.name, :related_side => 'destination', :relationship_type => dependence_relationship_type) }
            %i.grcicon-link
        -if has_process_relationship
          %a.btn.btn-mini{ :rel => "tooltip", :"data-placement" => "left", :title => "Link processes to this object", :'data-modal-class' => 'modal-wide', :'data-modal-reset' => 'reset', :'data-list-target' => 'refresh', :"data-toggle" => "modal-ajax-relationship-selector", :href => list_edit_flow_relationships_path(:object_id => instance.id, :object_type => instance.class.name, :related_model => 'Process', :related_side => 'destination', :relationship_type => process_relationship_type) }
            %i.grcicon-link
        -if !is_root
          %a.btn.btn-mini{ "rel" => "tooltip", "data-placement" => "left", "title" => "Go to this object's page", :href => url_for(:id => instance.id, :action => :show, :controller => model_underscore.pluralize, :only_path => true) }
            %i.grcicon-goto
  .item-content{ :class => is_root && 'in' }
    -if !seen_this
      -if has_process_relationship && systems.count > 0
        %ul.tree-structure
          -systems.each_with_index do |system, i|
            =render :partial => 'system_tree', :locals => { :system => system, :id_prefix => "#{id_prefix}-system-#{system.id}" }

      -if has_dependence_relationship && sub_objects.count > 0
        %ul.tree-structure
          -sub_objects.each_with_index do |sub_object, i|
            =render :partial => 'object_tree', :locals => { :instance => sub_object, :id_prefix => "#{id_prefix}-sub-object-#{sub_object.id}", :seen_ids => seen_ids + [instance.id] }
